package demo_0427;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MapDemo {
    public static void main(String[] args) {
        Map<String, Objects> map = new HashMap<>();
    }

    //投票计数问题
    public static void main2(String[] args) {
        Map<String,Integer> votes = new HashMap<>();
        votes.put("A",0);
        votes.put("B",0);
        votes.put("C",0);
        for (int i = 0; i < 100; i++) {
            int random = (int) (Math.random()*3);
            String candidate = ""   ;
            switch (random) {
                case 0:
                    candidate = "A";
                    break;
                case 1:
                    candidate = "B";
                    break;
                case 2:
                    candidate = "C";
                    break;

            }
            votes.put(candidate, votes.get(candidate)+1);
        }
        System.out.println(votes);
        int maxVotes = 0;
        String winner = "";
        for (Map.Entry<String,Integer> entry : votes.entrySet()) {
            if (entry.getValue()>maxVotes) {
                maxVotes = entry.getValue();
                winner = entry.getKey();
            }
        }
        System.out.println("获胜者是："+winner);
    }


    public static void main1(String[] args) {
        //使用HashMap存储候选人和票数
        Map<String,Integer> votes = new HashMap<>();
        //初始化候选人
        votes.put("A",0);
        votes.put("B",0);
        votes.put("C",0);
        //模拟投票过程
        String[] candidates = {"A","B","C","A","B","C","A","B","C","A"};
        for (String candidate : candidates) {
            //如果候选人存在于Map中，票数加1
            if (votes.containsKey(candidate)) {
                votes.put(candidate, votes.get(candidate)+1);
            }

        }
        //输出结果
        System.out.println("候选人\t票数");
        for (Map.Entry<String,Integer> entry : votes.entrySet()) {
            System.out.println(entry.getKey()+"\t"+entry.getValue());
        }
        //找出票数最多的候选人
        int maxVotes = 0;
        String winner = "";
        for (Map.Entry<String,Integer> entry : votes.entrySet()) {
            if (entry.getValue()>maxVotes) {
                maxVotes = entry.getValue();
                winner = entry.getKey();
            }
        }
        System.out.println("获胜者是："+winner);
    }
}
